<template>
  <el-dialog
    :title="!dataForm.examCode ? '新增' : '考试'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="140px"
    >
    <el-form-item label="考试编码" prop="examCode">
        <el-input :disabled="true" v-model="dataForm.examCode" placeholder="考试名称"></el-input>
      </el-form-item>
      <el-form-item label="考试名称" prop="examName">
        <el-input :disabled="true" v-model="dataForm.examName" placeholder="考试名称"></el-input>
      </el-form-item>
        <el-form-item label="考试内容" prop="examContent"></el-form-item>
        <el-input
          type="textarea"
          :rows="5"
          placeholder="请输入内容"
          v-model="dataForm.examContent"
        ></el-input>
    
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">提交试卷</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        examCode: "",
        examName: "",
        examContent: "",
        examAddress: "",
        examType: "",
      },
      dataRule: {

      },
    };
  },
  methods: {
    init(id) {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        this.dataForm.examCode = id || 0;
        if (this.dataForm.examCode) {
          this.$http({
            url: this.$http.adornUrl(
              `/exam/texaminformationdetail/info/${this.dataForm.examCode}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.examName = data.tExamInformationDetail.examName;
              this.dataForm.examContent =
                data.tExamInformationDetail.examContent;
              this.dataForm.examAddress =
                data.tExamInformationDetail.examAddress;
              this.dataForm.examType = data.tExamInformationDetail.examType;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/exam/texaminformationdetail/${
                !this.dataForm.examCode ? "save" : "update"
              }`
            ),
            method: "post",
            data: this.$http.adornData({
              examCode: this.dataForm.examCode || undefined,
              examName: this.dataForm.examName,
              examContent: this.dataForm.examContent,
              examAddress: this.dataForm.examAddress,
              examType: this.dataForm.examType,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
